package ru.niir.goodfil.ui;

import org.apache.log4j.Logger;
import ru.niir.goodfil.db.Good;

import javax.faces.context.FacesContext;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class DetailedGoodAbsenceBean extends PageBean {

    private static Logger logger = Logger.getLogger(DetailedGoodAbsence.class);

    private int rowsCount = 0;

    private Date beginDate;

    private Date endDate;

    private Good good;

    private double sum = 0;

    public DetailedGoodAbsenceBean() {
        init();
    }

    public void init(){
        Map sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        GoodAbsenceReportBean reportBean = (GoodAbsenceReportBean)sessionMap.get("GoodAbsenceReportBean");
        if(reportBean != null){
            good = reportBean.getGood();
            beginDate = reportBean.getBeginDate();
            endDate = reportBean.getEndDate();
        }
        rowsCount = getGoodAbsence().size();
    }

    public List<DetailedGoodAbsence> getGoodAbsence(){
        if(beginDate.after(endDate)){
            facesError("Начальная дата не может быть больше конечной");
        }
        List list = gf().getDetaildeInfo(good.getId(), beginDate, endDate);
        List<DetailedGoodAbsence> result = new ArrayList<DetailedGoodAbsence>();
        for(Object o : list){
            Object[] arr = (Object[])o;
            try {
                long idManager = ((BigInteger)arr[0]).longValue();
                long idClient = ((BigInteger)arr[1]).longValue();
                int inquriesCount = ((BigInteger)arr[2]).intValue();
                int goodAbsenceCount = ((BigDecimal)arr[3]).intValue();

                DetailedGoodAbsence absence = new DetailedGoodAbsence();
                absence.setManagerFio(gf().getUserById(idManager).getFio());
                absence.setCompanyMane(gf().getClientById(idClient).getCompanyName());
                absence.setInquriesCount(inquriesCount);
                absence.setGoodAbsenceCount(goodAbsenceCount);
                absence.setSum(gf().getPrice2(good.getId(), good.getBrandId(), idClient, gf().getClientById(idClient).getClientgroupIdByBrandId(1)).getPrice() * goodAbsenceCount);
                result.add(absence);
            } catch (Exception e) {
                logger.error("Exception: " + e.getMessage());
            }
        }
        return result;
    }

    public int getRowsCount() {
        return rowsCount;
    }

    public void setRowsCount(int rowsCount) {
        this.rowsCount = rowsCount;
    }

    public String getBeginDate() {
        SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
        return format.format(beginDate);
    }

    public String getEndDate() {
        SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
        return format.format(endDate);
    }

    public Good getGood() {
        return good;
    }

    public void setGood(Good good) {
        this.good = good;
    }

    public double getSum() {
        List<DetailedGoodAbsence> result = getGoodAbsence();
        for(DetailedGoodAbsence absence : result){
            sum +=absence.getSum();
        }
        return sum;
    }

    public class DetailedGoodAbsence{
        public String managerFio = "";
        public String companyMane = "";
        public int inquriesCount = 0;
        public int goodAbsenceCount = 0;
        public double sum = 0;

        public DetailedGoodAbsence() {
        }

        public String getManagerFio() {
            return managerFio;
        }

        public void setManagerFio(String managerFio) {
            this.managerFio = managerFio;
        }

        public String getCompanyMane() {
            return companyMane;
        }

        public void setCompanyMane(String companyMane) {
            this.companyMane = companyMane;
        }

        public int getInquriesCount() {
            return inquriesCount;
        }

        public void setInquriesCount(int inquriesCount) {
            this.inquriesCount = inquriesCount;
        }

        public int getGoodAbsenceCount() {
            return goodAbsenceCount;
        }

        public void setGoodAbsenceCount(int goodAbsenceCount) {
            this.goodAbsenceCount = goodAbsenceCount;
        }

        public double getSum() {
            return sum;
        }

        public void setSum(double sum) {
            this.sum = sum;
        }
    }
}
